package com.sptpc.springbootmybatis.service;

import com.sptpc.springbootmybatis.dao.CommodityMapper;
import com.sptpc.springbootmybatis.model.Commodity;
import com.sptpc.springbootmybatis.vo.CommodityVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommodityService {
    @Autowired
    private CommodityMapper commodityMapper;

    public Object saveCommodity(CommodityVo commodityVo) {
        Commodity commodity = new Commodity();
        commodity.setFicationId(commodityVo.getFicationId());
        commodity.setName(commodityVo.getName());
        commodity.setCreateTime(new Date());
        commodity.setUpdateTime(new Date());
        commodity.setPrice(commodityVo.getPrice());
        if(commodityMapper.insertSelective(commodity)!=0){
            return findByFicationIdAndName(commodityVo.getFicationId(),commodityVo.getName());
        }
        return null;
    }

    //写一个方法，把CommodityVo转换为Commodity
    private Commodity swap(CommodityVo commodityVo){
        Commodity commodity = new Commodity();
        commodity.setId(commodityVo.getId());
        commodity.setFicationId(commodityVo.getFicationId());
        commodity.setName(commodityVo.getName());
        commodity.setPrice(commodityVo.getPrice());
        commodity.setUpdateTime(new Date());
        return commodity;
    }

}
